﻿namespace jq9602_jq9603.Process
{
    public class BinaryPulseDetector
    {
        // 配置参数
        public static int MinPulseWidth = 3;    // 脉冲最小宽度（连续1的最少个数）

        public static int MinGapWidth = 5;      // 双峰间最小间隔（连续0的最少个数）
        public static int RequiredPulseCount = 2; // 需要检测的脉冲数量

        public static bool HasExactlyPulses(List<int> data)
        {
            var pulses = FindAllPulses(data);
            //return pulses.Count == RequiredPulseCount && ValidateGap(data, pulses);
            return pulses.Count == RequiredPulseCount;
        }

        public static List<PulseInfo> FindAllPulses(List<int> data)
        {
            var pulses = new List<PulseInfo>();
            bool inPulse = false;
            int pulseStart = 0;

            for (int i = 0; i < data.Count; i++)
            {
                if (data[i] == 1)
                {
                    if (!inPulse) // 脉冲开始
                    {
                        inPulse = true;
                        pulseStart = i;
                    }
                }
                else // data[i] == 0
                {
                    if (inPulse) // 脉冲结束
                    {
                        // 记录有效脉冲（宽度≥MinPulseWidth）
                        if (i - pulseStart >= MinPulseWidth)
                        {
                            pulses.Add(new PulseInfo
                            {
                                Start = pulseStart,
                                End = i - 1,
                                Width = i - pulseStart
                            });
                        }
                        inPulse = false;
                    }
                }
            }

            // 处理数据末尾的脉冲
            if (inPulse && data.Count - pulseStart >= MinPulseWidth)
            {
                pulses.Add(new PulseInfo
                {
                    Start = pulseStart,
                    End = data.Count - 1,
                    Width = data.Count - pulseStart
                });
            }

            return pulses;
        }

        private static bool ValidateGap(List<int> data, List<PulseInfo> pulses)
        {
            if (pulses.Count != 2) return false;

            // 检查两脉冲间的间隔
            int gapStart = pulses[0].End + 1;
            int gapEnd = pulses[1].Start - 1;
            int gapWidth = gapEnd - gapStart + 1;

            // 验证间隔宽度和内容
            if (gapWidth < MinGapWidth) return false;

            for (int i = gapStart; i <= gapEnd; i++)
            {
                if (data[i] != 0) return false;
            }

            return true;
        }

        // 高级检测：验证脉冲形态一致性
        public static bool CheckPulseUniformity(List<PulseInfo> pulses, double tolerance = 0.2)
        {
            if (pulses.Count != 2) return false;

            double ratio = (double)pulses[0].Width / pulses[1].Width;
            return ratio >= 1 - tolerance && ratio <= 1 + tolerance;
        }
    }

    public class PulseInfo
    {
        public int Start { get; set; }  // 脉冲起始索引
        public int End { get; set; }    // 脉冲结束索引
        public int Width { get; set; }  // 脉冲宽度（点数）

        public override string ToString() =>
            $"脉冲[{Start}-{End}] 宽度={Width}";
    }
}